package com.gy.mbaselibrary.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.gy.mbaselibrary.net.BaseRequest;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/1/31
 * 类描述：gragment基类
 * 修改人：
 * 修改时间：2018/1/31
 * 修改备注：
 */
public abstract class BaseFragment extends Fragment implements BaseRequest.ActivityResponseListener {
    protected View view;
    protected final String TAG_LOAD = "load";
    protected final String TAG_REFRESH = "refresh";
    //当前页,每页显示的条数
    protected int currentPage = 1, pageSize = 20;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        if (null != view) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (null != parent) {
                parent.removeView(view);
            }
        } else {
            view = layoutInflater.inflate(setLayout(), null);
        }
        ButterKnife.bind(this, view);
        initViews(view, bundle);
        setData(bundle);
        setListeners();
        return view;
    }

    public abstract void initViews(View view, Bundle bundle);

    public abstract void setData(Bundle bundle);

    public abstract void setListeners();

    public abstract int setLayout();

    /**
     * 其他设备登录
     */
    protected abstract void otherLogin();


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
    }

    //跳转页面的基础方法
    protected void goToBase(Class clz, Bundle bundle, int requestCode, boolean isFinish) {
        Intent intent = new Intent(getActivity(), clz);
        //设置bundle
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        //设置requestcode
        startActivityForResult(intent, requestCode);
        //动画效果
        //overridePendingTransition(R.anim.activity_open_common, 0);
        //设置是否结束当前页面
        if (isFinish) {
            getActivity().finish();
        }
    }

    public void goTo(Class clz) {
        goTo(clz, null);
    }

    public void goTo(Class clz, Bundle bundle) {
        goToBase(clz, bundle, -1, false);
    }

    public void goTo(Class clz, int requestCode) {
        goToBase(clz, null, requestCode, false);
    }

    public void goTo(Class clz, int requestCode, Bundle bundle) {
        goToBase(clz, bundle, requestCode, false);
    }

    public void goToAndFinish(final Class clz) {
        goToBase(clz, null, -1, true);
    }

    public void goToAndFinish(final Class clz, Bundle bundle) {
        goToBase(clz, bundle, -1, true);
    }

    protected Context getCurrentContext() {
        return getActivity();
    }

}
